<?php

class MainWindow extends WBResizableWindow { 
	
	private $_toolDialog = null;
	private $_project = null;
	private $_statusBar = null;
	private $_resourceManager = null;
	
	public function __construct() {

		$screen  = WBSystem::getAvailableScreen();
		
		parent::__construct(array(
			'title' => 'WinBinder Form Editor (OOP Version)',
			'name' => 'MainWindow',
			'width' => 300,
			'height' => $screen['height'],
			'icon' => RESOURCE_HOME."wb.ico"
		));
	}
	
	public function __destruct() { 
		parent::__destruct();
		
		unset($this->_toolDialog);
		unset($this->_project);
		unset($this->_statusBar);
	}
	
	public function initComponent() { 
		parent::initComponent();

		// set minimized size 
		$this->setMinimumSize(300, 575);
		
		// set control 
		$this->createMainMenu();		
		$this->createMainToolBar();
		$this->createMainStatusBar();			
		$this->createControlPanel();
		
		// run accelerator
		$this->runAccel();
		
		$this->_project = new ProjectManager();
		$this->_resourceManager = new ResourceManager();
		
		$this->createProject();
		
		$this->createTimer(array($this, 'checkMemory'), 1000);
	}
	
	public function getSystemMemory() { 
		$max = memory_get_peak_usage();
		$use = memory_get_usage();

		$temp = "$use/$max";
		
		return $temp;
	}
	
	
	public function checkMemory($eo) { 
		$memory = $this->getSystemMemory();
		$this->_statusBar->setText($memory, 1);
	}	
	
	/**
	 * 생성된 프로젝트 key 전달 
	 * 
	 * @return string created project key 
	 */
	public function createProject($eo = null) { 
		return $this->_project->createProject();
	}
	
	public function refreshProject($eo = null) {
		return $this->_project->refreshProject();
	}	
	
	public function createControlInProject($type) { 
		return $this->_project->createControl($type);
	}

	/**
	 * 
	 * @return ProjectManager
	 */
	public function getProjectManager() { 
		return $this->_project;
	}
	
	public function getResourceManager() { 
		return $this->_resourceManager;
	}
	
	public function setResource($key, $type, $data) { 
		return $this->_resourceManager->setResource($key, $type, $data);	
	}
	
	public function getResource($key) { 
		return $this->_resourceManager->getResource($key);	
	}
	
	public function deleteResource($key) { 
		return $this->_resourceManager->deleteResource($key);	
	}	
	
	public function createMainMenu() { 
		$this->createMenu(array(
			'&File',
				array('caption' => "&New project\tCtrl+N", 'accel' => 'Ctrl+N', 'callback' => array($this, 'createProject')),
				array('caption' => "&Open project...\tCtrl+O", 'accel' => 'Ctrl+O'),
				null,
				array('caption' => "&Save project\tCtrl+S", 'accel' => 'Ctrl+S'),
				array('caption' => "Save project &As...\tCtrl+Shift+S", 'accel' => 'Ctrl+Shift+S'),
				array('caption' => "Export &Project code...\tCtrl+Shift+E", 'accel' => 'Ctrl+Shift+E'),
				null,
				array('caption' => "E&xit\tAlt+F4", 'accel' => 'Alt+F4'),
			'&Edit',
				array('caption' => "&Delete control\tCtrl+Del", 'accel' => 'Ctrl+Del'),
				null,
				array('caption' => "Move forwards\tCtrl+PgDn", 'accel' => 'Ctrl+PgDn'),
				array('caption' => "Move backwords\tCtrl+PgUp", 'accel' => 'Ctrl+PgUp'),
				null,
				array('caption' => "Project properties...\tCtrl+Shift+P", 'accel' => 'Ctrl+Shift+P'),
				null,
				array('caption' => "Options...\tCtrl+Shift+O", 'accel' => 'Ctrl+Shift+O'),
			'&View',
				array('caption' => "Controls window", 'selected' => true, 'callback' => array($this, 'toggleControlWindow')),
				null,
				array('caption' => "&Preview form...", 'accel' => 'F9'),
			'&Help',
				array('caption' => "&Web site\tCtrl+Shift+W", 'accel' => 'Ctrl+Shift+W'),
				array('caption' => "&EasyLogic site\tCtrl+Shift+L", 'accel' => 'Ctrl+Shift+L'),
				null,
				array('caption' => '&About this program...')
		));
	}
	
	public function toggleControlWindow($eo) {

		if ($this->_toolDialog) {
			$eo->object()->selected = false;
			
			$this->_toolDialog->close();
			$this->_toolDialog = null;
		} else { 
			$eo->object()->selected = true;
			
			$this->createToolDialog();
		}
		
	}
	
	public function createMainToolBar() { 
		$this->createToolBar(array(
			'items' => array(
				null,
				array('tooltip' => 'New Project (Ctrl+N)', 'image' => 0),
				array('tooltip' => 'Open Project (Ctrl+O)', 'image' => 1),
				array('tooltip' => 'Save Project (Ctrl+S)', 'image' => 2),
				null,
				array('tooltip' => 'Preview form (F9)', 'image' => 22),
				null,
				array('tooltip' => 'Delete (Del)', 'image' => 12),				
				null,
				array('tooltip' => 'Web site', 'image' => 11),
			),
			'image' => RESOURCE_HOME."toolbar.bmp"
		));
	}
	
	public function createMainStatusBar() { 
		$this->_statusBar = $this->createStatusBar(array(
			'items' => array(
				'',
				array('memory', 150)
			)
		));
	}
	
	public function createControlPanel() { 
		
		$this->setLayout('vbox');
		
		$this->add(new MainControlPanel(array(
			'parent' => $this,
			'name' => 'ControlPanel',
			'width' => 280,
		)));
	}
}

?>